<?php namespace My\Field\Quan;

class Yonghuzu extends \Phpcmf\Library\A_Field {
	
	/**
     * 构造函数
     */
    public function __construct() {
        parent::__construct();
		$this->fieldtype = ['TEXT' => '']; // TRUE表全部可用字段类型,自定义格式为 array('可用字段类型名称' => '默认长度', ... )
		$this->defaulttype = 'TEXT'; // 当用户没有选择字段类型时的缺省值
    }
	
	/**
	 * 字段相关属性参数
	 *
	 * @param	array	$value	值
	 * @return  string
	 */
	public function option($option) {

		return [''];
	}

    /**
     * 创建sql语句
     */
	/*public function create_sql($name, $value, $cname = '') {
	    $sql = 'ALTER TABLE `{tablename}` ADD `'.$name.'` TEXT NULL , ADD `'.$cname.'` TEXT NULL';
	    return $sql;
	}*/

    /**
     * 修改sql语句
     */
    public function alter_sql($name, $value, $cname = '') {
        return NULL;
    }

    /**
     * 删除sql语句
     */
    public function drop_sql($name) {
        return 'ALTER TABLE `{tablename}` DROP `'.$name.'`';
    }
	
	/**
	 * 字段输出
	 */
	public function output($value) {
		return $value;
	}
	
	/**
	 * 字段入库值
	 */
	public function insert_value($field) {
		$zus = \Phpcmf\Service::L('field')->post[$field['fieldname']];
		if($zus) {
			$zu = [
			'zuid' => $zus['zuid'],
			];
			
			\Phpcmf\Service::L('field')->data[$field['ismain']][$field['fieldname']] = dr_array2string($zu);
		}
		

	}

    /**
     * 字段表单输入
     *
     * @return  string
     */
    public function input($field, $value = []) {

        // 字段禁止修改时就返回显示字符串
        /*if ($this->_not_edit($field, $value)) {
            return $this->show($field, $value);
        }*/

        // 字段存储名称
        $name = $field['fieldname'];

        // 字段显示名称
        $text = ($field['setting']['validate']['required'] ? '<span class="required" aria-required="true"> * </span>' : '').$field['name'];

        // 表单附加参数
        $attr = $field['setting']['validate']['formattr'];

        // 字段提示信息
        $tips = ($name == 'title' && APP_DIR) || $field['setting']['validate']['tips'] ? '<span class="help-block" id="dr_'.$field['fieldname'].'_tips">'.$field['setting']['validate']['tips'].'</span>' : '';

        // 是否必填
        $required =  $field['setting']['validate']['required'] ? ' required="required"' : '';



        // 字段默认值
        $value = dr_string2array(\Phpcmf\Service::L('Field')->value[$field['fieldname']]);
		$uriprefix = APP_DIR.'/'.\Phpcmf\Service::L('Router')->class;
	

        // 表单选项
        //$str = '<label><select '.$required.' class="form-control '.$field['setting']['option']['css'].'" name="data['.$name.']" id="dr_'.$name.'_ting" '.$attr.' >';
        //$str.= '<option value="0">'.dr_lang('选择分类').'</option>';
        //$options = \Phpcmf\Service::M('content', 'www')->son_type(\Phpcmf\Service::C()->index['son_type']);
		$wo = \Phpcmf\Service::C()->index['uid'];
		$catid = \Phpcmf\Service::C()->index['catid'];
		$uid = \Phpcmf\Service::C()->uid;
		$id = $_GET['id'];
		
		//dr_debug('id', $id);
		$yonghuzu = \Phpcmf\Service::M()->table('1_'.MOD_DIR.'_form_yonghuzu')->where('cid', $id)->getAll();
		//dr_debug('id', $yonghuzu.'m-'.MOD_DIR.'--'.$member.id.'');
		//dr_debug('yonghuzu', $yonghuzu);
		$_option = '';
		foreach ($yonghuzu as $dir => $t) {
			$dir = $dir + 1;
			$_option.= '<option value="'.$t['id'].'" '.($value['zuid'] == $t['id'] ? 'selected' : '').'>'.$t['title'].'</option>';
		}

		$strs.= '
		<label>
			<select  '.$required.' class="form-control '.$field['setting']['option']['css'].'" name="data['.$name.'][zuid]" id="dr_'.$name.'_zuid" '.$attr.' >
			
				'.$_option.'
			</select>
		</label>
		<a href="javascript:dr_iframe(\''.dr_lang('添加用户组').'\', \''.dr_url(MOD_DIR.'/yonghuzu/add').'&cid='.$id.'\', \'90%\',\'80%\')" class="btn btn-xs dark"> <i class="fa fa-users"></i> '.dr_lang('添加用户组').'</a>
		<a href="javascript:dr_iframe(\''.dr_lang('管理用户组').'\', \''.dr_url(MOD_DIR.'/yonghuzu/index').'&cid='.$id.'\', \'90%\',\'80%\')" class="btn btn-xs">  '.dr_lang('管理用户组').'</a>
		';
		$strb.= '
		<label>
			<select  '.$required.' class="form-control '.$field['setting']['option']['css'].'" name="data['.$name.'][zuid]" id="dr_'.$name.'_zuid" '.$attr.' >
			
				'.$_option.'
			</select>
		</label>
		<a href="javascript:dr_iframe(\''.dr_lang('添加用户组').'\', \''.dr_url(MOD_DIR.'/yonghuzu/add').'&cid='.$id.'\', \'90%\',\'80%\')" class="btn btn-xs dark"> <i class="fa fa-users"></i> '.dr_lang('添加用户组').'</a>
		
		<a onClick="d_add_yonghuzu()" data-id="'.$id.'"  class="btn btn-xs">  '.dr_lang('插入默认用户组').'</a>
		<script type="text/javascript">
		function d_add_yonghuzu() {
			var id=$(this).data("id");
			$.ajax({
			    type: "GET",
			    url: "'.dr_url(MOD_DIR.'/yonghuzu/morenadd').'&cid='.$id.'&uid='.$uid.'&catid='.$catid.'",
			    dataType: "json",
			    success: function(json){
					dr_cmf_tips(1, json.msg);
					setTimeout("window.location.reload(true)");
				}
			});
		}
		</script>
		';
		if($_GET['m']=='edit' && !$yonghuzu){
			$str.= $strb;
		} else {
			$str.= $strs;
		}

       
        return $this->input_format($name, $text, $str);
    }



    /**
     * 字段表单显示
     *
     * @param	string	$field	字段数组
     * @param	array	$value	值
     * @return  string
     */
    public function show($field, $value = null) {

        $options = dr_format_option_array($field['setting']['option']['options']);

        $str = '<div class="form-control-static"> '.(isset($options[$value]) ? $options[$value] : dr_lang('未选择')).' </div>';

        return $this->input_format($field['fieldname'], $field['name'], $str);
    }
}